<template>
  <d2-container class="page">
    <el-dialog
      width="800px"
      title="区域数据"
      :visible.sync="dialogFormVisible2"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <el-form :model="form2" ref="form2">
        <el-form-item label="邮编">
          <el-cascader :props="props" v-model="form2.postCode" ref="myCascader" style="width:400px"></el-cascader>
        </el-form-item>
        <el-form-item label="首重运费">
          <el-input v-model="form2.defaultFee"></el-input>
        </el-form-item>
        <el-form-item label="次重运费">
          <el-input v-model="form2.increaseFee"></el-input>
        </el-form-item>
        <el-form-item label="这个地区满多少免运费">
          <el-input v-model="form2.fullFee"></el-input>
        </el-form-item>
        <el-form-item label="运费说明">
          <el-input type="textarea" v-model="form2.memo"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible2 = false">取 消</el-button>
        <el-button type="primary" @click="handleCreateNew2()">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog
      width="1024px"
      :title="createOrEdit"
      :visible.sync="dialogFormVisible"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <el-form :model="form" ref="form">
        <el-form-item label="名称">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="默认运费">
          <el-input v-model="form.defaultFee"></el-input>
        </el-form-item>
        <el-form-item label="默认满多少免运费">
          <el-input v-model="form.fullFee"></el-input>
        </el-form-item>
        <el-form-item label="运费说明">
          <el-input type="textarea" v-model="form.info"></el-input>
        </el-form-item>
        <el-form-item label="适用地区" v-if="createOrEdit == '编辑'">
          <br />
          <el-button type="primary" size="mini" @click="showCreateDlg2()">新建</el-button>
          <el-button type="primary" size="mini" @click="getList2()">刷新</el-button>
          <el-divider></el-divider>
          <el-table
            stripe
            ref="multipleTable"
            :data="tableData2"
            tooltip-effect="dark"
            style="width: 100%"
          >
            <!-- <el-table-column type="selection" width="55"></el-table-column> -->
            <!-- <el-table-column type="index" label="序号"></el-table-column> -->
            <el-table-column label="ID" width="120">
              <template slot-scope="scope">{{ scope.row.id }}</template>
            </el-table-column>
            <el-table-column label="地区" width="250">
              <template slot-scope="scope">{{ scope.row.adName }}</template>
            </el-table-column>
            <el-table-column label="邮编">
              <template slot-scope="scope">{{ scope.row.zipCode }}</template>
            </el-table-column>
            <el-table-column label="首重运费">
              <template slot-scope="scope">{{ scope.row.defaultFee }}</template>
            </el-table-column>
            <el-table-column label="次重运费">
              <template slot-scope="scope">{{ scope.row.increaseFee }}</template>
            </el-table-column>
            <el-table-column label="这个地区满多少免运费" width="200">
              <template slot-scope="scope">{{ scope.row.increaseFee }}</template>
            </el-table-column>
            <el-table-column label="运费说明">
              <template slot-scope="scope">{{ scope.row.memo }}</template>
            </el-table-column>
            <el-table-column align="center" label="操作" width="150" fixed="right">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="primary"
                  @click="showEditDlg2(scope.row)"
                  icon="el-icon-edit"
                ></el-button>
                <el-button
                  size="mini"
                  type="danger"
                  @click="handleDelete4Area(scope.row)"
                  icon="el-icon-delete"
                ></el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleCreateNew('form')">确 定</el-button>
      </div>
    </el-dialog>
    <el-button type="primary" size="mini" @click="showCreateDlg('form')">新建</el-button>
    <el-button type="primary" size="mini" @click="getList()">刷新</el-button>
    <el-divider></el-divider>
    <el-table
      stripe
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      style="width: 100%"
    >
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column type="index" label="序号"></el-table-column>
      <el-table-column label="ID" width="120">
        <template slot-scope="scope">{{ scope.row.id }}</template>
      </el-table-column>
      <el-table-column label="名称" width="250">
        <template slot-scope="scope">{{ scope.row.name }}</template>
      </el-table-column>
      <el-table-column label="默认运费">
        <template slot-scope="scope">{{ scope.row.defaultFee }}</template>
      </el-table-column>
      <el-table-column label="默认满多少免运费">
        <template slot-scope="scope">{{ scope.row.fullFee }}</template>
      </el-table-column>
      <el-table-column label="运费说明" width="250">
        <template slot-scope="scope">{{ scope.row.info }}</template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="showEditDlg(scope.row)" icon="el-icon-edit"></el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row)"
            icon="el-icon-delete"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <br />
    <el-pagination background layout="prev, pager, next" :total="50"></el-pagination>
  </d2-container>
</template>

<script>
import * as freight from "@/api/shop.freight";
import * as catalog from "@/api/shop.catalog";

let pid = 0;

export default {
  name: "freight",
  components: {},
  data() {
    return {
      currentFreightId: null,
      props: {
        lazy: true,
        lazyLoad(node, resolve) {
          if (node.level > 0) {
            pid = node.data.id;
          }

          freight.getPostCode(pid).then(resp => {
            console.log(resp);

            let nodes = [];
            resp.forEach(element => {
              nodes.push({
                id: element.id,
                value: element.adcode,
                label: element.name
              });
            });

            resolve(nodes);
          });
        }
      },

      dialogFormVisible2: false,
      skuList: null,
      catalogList: null,
      dialogImageUrl: "",
      fileList: [],
      tableData: [],
      tableData2: [],
      multipleSelection: [],
      dialogFormVisible: false,
      form: {},
      form2: {},
      formLabelWidth: "100px",
      dialogVisible: false,
      createOrEdit: "",
      listQuery: {
        pageStart: 1,
        pageSize: 25
      }
    };
  },
  mounted() {
    this.$route.meta.title = "运费管理";
    this.getList();
  },
  methods: {
     showEditDlg2(row) {
      this.dialogFormVisible2 = true;
      this.form2 = row;
    },

    showCreateDlg2() {
      this.dialogFormVisible2 = true;
      this.form2.freightId = this.form.id;
    },

    onSelectedChange(e) {
      console.log("onSelectedChange", e);
      this.listQuery.categoryId = e;
      swiper.getSkuList(this.listQuery).then(response => {
        if (response.success) this.skuList = response.data.list;
      });
    },

    handleUploadSuccess(response, file, fileList) {
      console.log("handleUploadSuccess", response);
      
      if (response.success) this.fileList = [];
      this.fileList.push({
        url: response.downloadPath
      });
    },

    handleRemove(file, fileList) {
      console.log(file, fileList);
      const index = this.fileList.indexOf(file, 0);
      if (index > -1) {
        this.fileList.splice(index, 1);
      }
    },

    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },

    showCreateDlg(formName) {
      this.dialogFormVisible = true;
      this.createOrEdit = "新建";
    },

    showEditDlg(row) {
      this.dialogFormVisible = true;
      this.createOrEdit = "编辑";
      this.form = row;
      this.currentFreightId = row.id;
      
      this.getList2();
    },

    handleUploadSuccess(response, file, fileList) {
      console.log(response);
      if (response.success) this.downloadPath = response.downloadPath;
    },

    handleCreateNew(formName) {
      let that = this;

      const set = that.$refs;
      set[formName].validate(valid => {
        if (valid) {
          freight.createOrEdit(that.form).then(resp => {
            if (resp.success) {
              that.dialogFormVisible = false;
              that.getList();
              that.$notify({
                title: "提示",
                message: "操作成功",
                type: "success",
                duration: 2000
              });
            } else {
              that.$notify({
                title: "提示",
                message: "操作失败",
                type: "error",
                duration: 2000
              });
            }
          });
        } else {
          return false;
        }
      });
    },

    handleCreateNew2() {
      let that = this;

      // 支持同时创建多个地区
      let obj = that.form2.postCode[that.form2.postCode.length - 1];
      that.form2.adcode = obj
      let tmp = this.$refs.myCascader.getCheckedNodes()[0].pathLabels;
      that.form2.adName = tmp[0] + ", " + tmp[1] + ", " + tmp[2];
      let freightRegionList = [];
      freightRegionList.push(that.form2);
      
      freight.createOrEdit4FreightRegion(freightRegionList).then(resp => {
        console.log(resp);
        if (resp.success) {
          that.dialogFormVisible2 = false;
          that.getList2();
          that.$notify({
            title: "提示",
            message: "操作成功",
            type: "success",
            duration: 2000
          });
        } else {
          that.$notify({
            title: "提示",
            message: "操作失败",
            type: "error",
            duration: 2000
          });
        }
      });
    },

    getList() {
      freight
        .fetchList({ queryCondition: JSON.stringify(this.listQuery) })
        .then(response => {
          if (response.success) this.tableData = response.data;
        });
    },

    getList2() {
      freight.getFreighRegiontList({ freighId: this.currentFreightId }).then(resp => {
        this.tableData2 = resp.data;
      });
    },

    handleRemove(file, fileList) {
      console.log(file, fileList);
    },

    handleDelete(row) {
      this.$confirm("确认删除么?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        freight.deleteById(row.id).then(resp => {
          if (resp.success) {
            this.getList();
            this.$notify({
              title: "提示",
              message: "操作成功",
              type: "success",
              duration: 2000
            });
          } else {
            this.$notify({
              title: "提示",
              message: resp.bizMessage,
              type: "error",
              duration: 2000
            });
          }
        });
      });
    },

    handleDelete4Area(row) {
      this.$confirm("确认删除么?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        freight.deleteById4FreighRegiont(row.id).then(resp => {
          if (resp.success) {
            this.getList2();
            this.$notify({
              title: "提示",
              message: "操作成功",
              type: "success",
              duration: 2000
            });
          } else {
            this.$notify({
              title: "提示",
              message: resp.bizMessage,
              type: "error",
              duration: 2000
            });
          }
        });
      });
    }
  }
};
</script>
